
const App = getApp()
const {LuoAnHttp} = App.globalData

const tabDataSource = [
  {title:'全部',value:'Q',dataSource:[],pagecurrent:1},
  {title:'烟雾浓度',value:'Y',dataSource:[],pagecurrent:1},
  {title:'电池电量',value:'D',dataSource:[],pagecurrent:1},
  {title:'电池温度',value:'W',dataSource:[],pagecurrent:1},]

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex:0,
    tabDataSource:tabDataSource
  },
  emptyOnClick:function(e){

  },
  toggleIndex:function(e){
    const detail = e.currentTarget;
    console.log(e);
    this.setData({
      currentIndex:detail.dataset.index
    })
  },
  loadData:function(index){
    this.loadAlarmList(this.currentParam(index))
  },


  currentParam:function(index){
    let current = this.data.tabDataSource[index]
    const param = {
      alarmtype:current.value,
      pagecurrent:current.pagecurrent,
    }
    return param
  },

  loadAlarmList:async function (param){
    const res = await LuoAnHttp.asyncAlarmQuery({...param,pagesize:10,stafftype:'N',userType:'Y'})
    if(res&&res.data&&res.data.records.length>0){
      let current = this.data.tabDataSource[this.data.currentIndex]
      current = {...current,dataSource:param.pagecurrent==1?[].concat(res.data.records):current.dataSource.concat(res.data.records),pagecurrent:param.pagecurrent}
      this.data.tabDataSource[this.data.currentIndex] = current;
      this.setData({
        tabDataSource:[].concat(this.data.tabDataSource)
      })
    }
  },


  onClick:function(e){
    wx.navigateTo({
      url: './alarmDetail',
      events:{
        callback:f=>this.loadData(this.data.currentIndex)
      },
      success:({eventChannel})=>{
        eventChannel.emit('param', e.detail)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadData(this.data.currentIndex)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const param = this.currentParam(this.data.currentIndex);
    this.loadAlarmList({...param,pagecurrent:1})
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const param = this.currentParam(this.data.currentIndex);
    this.loadAlarmList({...param,pagecurrent:1+param.pagecurrent})
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  swiperIndexChange:function(e){
    const detail = e.detail;
    const index = detail.current;
     
    if(index==this.data.currentIndex){
      this.loadData(detail.current)
    }else{
      this.loadData(detail.current)
      this.setData({
        currentIndex:index
      })
    }
    
    console.log('swiperIndexChange',e)
  }
})